/*
 * PopupImageViewerFx.cpp
 *
 *  Created on: Jul 6, 2013
 *      Author: guillou
 */

#include "PopupGui.hpp"
#include "PopupImage.hpp"
#include "PopupImageViewerFx.hpp"
#include "PopupSettings.hpp"


PopupTransitionFx::PopupTransitionFx(PopupImage *p_image)
: PopupImageFx(p_image),
  transition(PopupSettings::instance()->getImageViewerTransition()),
  xshift(0), yshift(0), speed(10)
{
  QRect _rect = QApplication::desktop()->availableGeometry();
  int _screenWidth  = _rect.width();
  int _screenHeight = _rect.height();

  if (transition.testFlag(PopupGui::POPUP_TRANSITION_FROM_TOP))
  {
    m_fxTarget->currentY = - m_fxTarget->currentImageHeight;
    yshift = speed;
  }
  else if (transition.testFlag(PopupGui::POPUP_TRANSITION_FROM_BOTTOM))
  {
    m_fxTarget->currentY = _screenHeight;
    yshift = -speed;
  }

  if (transition.testFlag(PopupGui::POPUP_TRANSITION_FROM_RIGHT))
  {
    m_fxTarget->currentX = -m_fxTarget->currentImageWidth;
    xshift = speed;
  }
  else if (transition.testFlag(PopupGui::POPUP_TRANSITION_FROM_LEFT))
  {
    m_fxTarget->currentX = _screenWidth;
    xshift = -speed;
  }

  // Adapt transition speed to make vertical & horizontal effects
  // synchronized
  if ((yshift != 0) && (xshift != 0))
  {
    if (m_fxTarget->currentImageWidth > m_fxTarget->currentImageHeight)
    {
      xshift *= ((float)m_fxTarget->currentImageWidth /
                 (float)m_fxTarget->currentImageHeight);
    }
    else
    {
      yshift *= ((float)m_fxTarget->currentImageHeight/
                 (float)m_fxTarget->currentImageWidth);
    }
  }
}

bool PopupTransitionFx::apply()
{
  bool _rc = false;

  if ((transition.testFlag(PopupGui::POPUP_TRANSITION_FROM_LEFT) &&
      (m_fxTarget->currentX > m_fxTarget->finalX))
      ||
      (transition.testFlag(PopupGui::POPUP_TRANSITION_FROM_RIGHT) &&
      (m_fxTarget->currentX < m_fxTarget->finalX)))
  {
    m_fxTarget->currentX += xshift;
    _rc = true;
  }
  if ((transition.testFlag(PopupGui::POPUP_TRANSITION_FROM_TOP) &&
      (m_fxTarget->currentY < m_fxTarget->finalY))
      ||
      (transition.testFlag(PopupGui::POPUP_TRANSITION_FROM_BOTTOM) &&
      (m_fxTarget->currentY > m_fxTarget->finalY)))
  {
    m_fxTarget->currentY += yshift;
    _rc = true;
  }

  return _rc;
}

PopupZoomFx::PopupZoomFx(PopupImage *p_image)
: PopupImageFx(p_image), xgrow(0.0), ygrow(0.0),
  x(0.0), y(0.0), w(0.0), h(0.0)
{
  xgrow = POPUP_MACRO_MAX(((float) m_fxTarget->finalImageWidth) / 100.0, 1.0);
  ygrow = POPUP_MACRO_MAX(((float) m_fxTarget->finalImageHeight) / 100.0, 1.0);
  apply();
}

bool PopupZoomFx::apply()
{
  bool _finalWidthReached = false;
  bool _finalHeightReached = false;

  // Set new size
  w += xgrow;
  h += ygrow;
  m_fxTarget->currentImageWidth  = (int) w;
  m_fxTarget->currentImageHeight = (int) h;

  // Handle limits!!
  if (m_fxTarget->currentImageWidth > m_fxTarget->finalImageWidth) {
    m_fxTarget->currentImageWidth = m_fxTarget->finalImageWidth;
    m_fxTarget->currentX = m_fxTarget->finalX;
    _finalWidthReached = true;
  }
  if (m_fxTarget->currentImageHeight > m_fxTarget->finalImageHeight) {
    m_fxTarget->currentImageHeight = m_fxTarget->finalImageHeight;
    m_fxTarget->currentY = m_fxTarget->finalY;
    _finalHeightReached = true;
  }

  // We're done!!
  if (_finalWidthReached && _finalHeightReached)
  {
    return false;
  }
  else
  {
    // Adjust position
    x = ((float)m_fxTarget->finalX) +
        ((((float)m_fxTarget->finalImageWidth) - w) / 2.0);
    y = ((float)m_fxTarget->finalY) +
        ((((float)m_fxTarget->finalImageHeight) - h) / 2.0);
    m_fxTarget->currentX = (int) x;
    m_fxTarget->currentY = (int) y;

    return true;
  }
}


bool PopupImageTextOverlayFx::paint(QPainter & p_painter)
{
#define MARGIN   70
#define FONTSIZE 40

  QString _sentBy = "> from " + m_fxTarget->m_sender + " <";

  // Set text font
  int _fontPointSize = FONTSIZE;
  QFont _font;
  _font.setPointSize(_fontPointSize);
  _font.setBold(true);
  _font.setWeight(200);
  p_painter.setFont(_font);

  // Rotate text, and translate it to appropriate position,
  // taking in account text rotation
  int _textW = p_painter.fontMetrics().width(_sentBy);
  for (int _i = _fontPointSize-1;
      (_textW > (m_fxTarget->currentImageWidth - 2*MARGIN)) && (_i > 0);
      _i--)
  {
    _font.setPointSize(_i);
    p_painter.setFont(_font);
    _textW = p_painter.fontMetrics().width(_sentBy);
  }
  p_painter.translate((m_fxTarget->currentImageWidth - _textW)/2,
                      m_fxTarget->currentImageHeight - MARGIN);

  // Draw background in white
  p_painter.setPen(QColor("#A3C2E0"));
  p_painter.drawText( 0,  2, _sentBy);
  p_painter.drawText( 0, -2, _sentBy);
  p_painter.drawText( 2,  2, _sentBy);
  p_painter.drawText( 2, -2, _sentBy);
  p_painter.drawText(-2,  0, _sentBy);
  p_painter.drawText(-2,  2, _sentBy);
  p_painter.drawText(-2, -2, _sentBy);

  // Draw text in black
  p_painter.setPen(QColor("#404040"));
  p_painter.drawText(0, 0, _sentBy);

  return true;
}
